<?php
require("session.php");

$flag=0;





			$name=$_POST['name'];
			$phone=$_POST['phone'];
			$email=$_POST['email'];
			$username=$_POST['username'];
			
			
			
			

			
			//Checking if the name is empty
			if ($name=='')
				{
					$_SESSION['a']="Please enter your name";
					header("Location:index.html");
					exit;
					
				}
			
			
			//Checking if the phone is non numeric
			if ($phone!="")
			{
					if (!is_numeric($phone))
					{
						$_SESSION['phone']="Phone number can only be numeric";
						header("Location:index.html");
					exit;
						
					}
					if((strlen($phone)<8) )
					{
						$_SESSION['phone_length']="Phone number cannot be less than 8 digits";
						header("Location:index.html");
					exit;
					}					
			}


			//Checking if address is empty
				if ($email=='')
				{
					$_SESSION['c']="Please enter Email";
					header("Location:index.html");
					exit;
				}
				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$email))
                                {
                                        $_SESSION['b']= "\n * Invalid email address";
                                        header("Location:form1.php");
					exit;
                                 }
				
				//Checking if massege is empty
				if ($username=='')
				{
					$_SESSION['d']="Please enter your username";
					header("Location:index.html");
					exit;
				}
				
		    
			
			
			
			else{
				require "conn.php";
					$checksql = "SELECT * FROM user WHERE username = '$username';";
					$checkresult = mysql_query($checksql);
					// if there a single user name existing in the database as entered by the user the checknumrows will consist of one or more than one rows
					$checknumrows = mysql_num_rows($checkresult);



					if($checknumrows >= 1) {
							$_SESSION['f']='Username already exists';
							
							header("Location: index.html");
							exit;
					}
		
				
				// If any of the above error exists then control is redirected to Refister page
				// Else we have to inert data into the customer table
				
	
					
					
					else{		
					
						//Inserting the data in customer Table
							 	$sql = "INSERT INTO `user`(`id`, `Name`, `Phone`, `email`, `username`) VALUES ('','$name','$phone','$email','$username')";
								

									$result=mysql_query($sql) or die(mysql_error());
									//Control is getting transferred on successful insertion
									
									
									if($result)
									{
										
										$_SESSION['in']= "Thank You ". $name." your Password will be sent in your mail..";
										$flag=2;
									}
									if($flag==2)
				{
					header("Location:index.html");
					exit;

				}
					}

				
					}
					




?>
